var parentData, parentActive;
layui.use(['form', 'layer'], function () {
	var form = layui.form,
		$ = layui.jquery;

	var imgPop;
	window.active = {
		addImg: () => {
			imgPop = top.layer.open({
				title: '新增详情图片',
				type: 2,
				area: ["920px", "550px"],
				fixed: false, //不固定
				offset: '50px',
				maxmin: false,
				content: 'page/goods/addImg.html',
				success: async function (layero, index) {
					let cw = layero.find('iframe')[0].contentWindow;
					cw.parentActive = active; //赋予父窗体事件
					setTimeout(function () {
						top.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
							tips: 3
						});
					}, 500);
				}
			});
		},
		saveImg: (imgurl) => {
			top.layer.close(imgPop);
			$('#Images').html($('#Images').html() +
				`<li><div class="uploadImgDiv"><img layer-src="${imgurl}" src="${imgurl}"></div><div class="operate"><i class="layui-icon img_del">&#xe640;</i></div></li>`);
		}
	};
	//操作
	$('.layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	form.verify({});
	form.on("submit(save)", element => {
		let data = form.val("dataform");
		data.cost_price = data.cost_price * 100;
		data.starting_price = data.starting_price * 100;
		data.protective_price = data.protective_price * 100;
		data.svaha_price = data.svaha_price * 100;
		data.bid_increment = data.bid_increment * 100;
		data.market_price = data.market_price * 100;
		let imgList = $('#Images').find('img');
		data.details = "";
		for (var i = 0; i < imgList.length; i++) {
			data.details += imgList[i].src + ';';
		}
		popSubmit('goods', data);
		return false;
	});

	//延时初始化，确保数据已经塞进来了
	setTimeout(() => {
		if (!parentData) return;
		// 开关禁用
		$('#checkbox').addClass("layui-disabled").attr("disabled", true);
		//初始化数据
		form.val("dataform", JSON.parse(JSON.stringify(parentData)));
		$('#tips_dt').html(`创建时间：${js.format_datetime(parentData.dt)}`);
		if (parentData.dt_freeze && new Date(parentData.dt_freeze) > Date.now()) {
			$('#tips_dt_freeze').html(`冻结至：${js.format_datetime(parentData.dt_freeze)}`);
		}
		//初始化图片
		var imgList = [],
			imgSource = parentData.details.split(';');

		for (var i = 0; i < imgSource.length; i++) {
			if (imgSource[i])
				imgList.push(`<li><div class="uploadImgDiv"><img layer-src="${imgSource[i]}" src="${imgSource[i]}"></div><div class="operate"><i class="layui-icon img_del">&#xe640;</i></div></li>`);
		}
		$('#Images').html(imgList.toString());
	}, 200);

	//图片预览弹出层
	$("body").on("click", ".uploadImgDiv img", function () {
		var imgSrc = $(this).attr('src');
		//页面层
		top.layer.open({
			type: 1,
			title: '',
			area: ['65%', '70%'], //宽高
			shadeClose: true, //开启遮罩关闭
			end: function (index, layero) {
				return false;
			},
			content: `<div style="height:100%;display: flex;display: -webkit-flex;flex-direction: column;justify-content: center;align-items: center;"><img style="max-width:95%;height:auto;width:55%;t" src="${imgSrc}" /></div>`
		});
	});
	//删除单张图片
	$("body").on("click", ".img_del", function () {
		var _this = $(this);
		top.layer.confirm('确定删除图片吗？', {
			icon: 3,
			title: '提示信息'
		}, function (index) {
			_this.parents("li").hide(1000);
			setTimeout(function () {
				_this.parents("li").remove();
			}, 950);
			top.layer.close(index);
		});
	});

});